package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.entity.Result;
import com.itheima.pojo.User;
import com.itheima.service.UserService;
import com.itheima.utils.HealthStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @description: 用户controller层
 * @author: wangjingliang
 * @create: 2019-11-25 00:45
 **/
@RestController
@RequestMapping("/user")
public class UserController {
    @Reference
    private UserService userService;
    @Autowired
    private BCryptPasswordEncoder passwordEncoder;
    /**
     * url:post("/user/add.do",this.formData)
     * @param:
     * @return: result
     * @date: 2019/11/25
     */
    @PostMapping("/add")
    public Result add(@RequestBody User user){

        if (HealthStringUtils.isEmpty(user.getUsername())||HealthStringUtils.isEmpty(user.getPassword())){
           return new Result(false,"用户名和密码不能为空");
        }
        user.setPassword(passwordEncoder.encode(user.getPassword()));

        try {
            userService.add(user);

        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,e.getMessage());
        }
        return new Result(true,"添加成功");
    }

}
